/*
 * Copyright (c) 2014 . All Rights Reserved.
 */

package com.cgmnx.admin.service.rbac.impl;

import com.cgmnx.admin.common.bean.Limiter;
import com.cgmnx.admin.dao.condition.UserCondition;
import com.cgmnx.admin.dao.entity.rbac.UserEntity;
import com.cgmnx.admin.dao.mapper.rbac.UserMapper;
import com.cgmnx.admin.model.enums.rbac.UserStatus;
import com.cgmnx.admin.service.rbac.UserService;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: Date: 14-3-6 Time: 下午3:30
 * @version: \$Id$
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

//    @Autowired
    private UserMapper userMapper;

    /**
     * 创建用户以及对应的角色映射关系
     *
     * @param user
     */
    @Override
    public Integer save(UserEntity user) {

        return userMapper.save(user);
    }

    @Override
    public int updateUser(UserEntity user) {
        return userMapper.update(user);
    }

    /**
     * 启用账号
     *
     * @param idList
     */
    @Override
    public int updateStatus(List<Integer> idList, UserStatus status, String updateUser) {

        return userMapper.updateStatus(idList, status, updateUser);
    }

    /**
     * 根据条件搜索用户信息
     *
     * @param userName
     * @return
     */
    @Override
    public List<UserEntity> queryUserByName(String userName) {

        if (StringUtils.isBlank(userName)) {
            return Lists.newArrayList();
        }

        UserCondition build = UserCondition.builder().userName(userName).build();

        return userMapper.queryUser(build);
    }

    /**
     * 根据账号名称获取用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public Optional<UserEntity> queryUserByUserId(String userId) {

        if (StringUtils.isBlank(userId)) {
            return Optional.absent();
        }

        UserCondition build = UserCondition.builder().userId(userId).build();
        List<UserEntity> userList = userMapper.queryUser(build);
        return CollectionUtils.isEmpty(userList) ? Optional.<UserEntity>absent() : Optional.of(userList.get(0));
    }

    /**
     * 根据账号名称获取用户信息
     *
     * @param email
     * @return
     */
    public Optional<UserEntity> queryUserByEmail(String email) {

        if (StringUtils.isBlank(email)) {
            return Optional.absent();
        }

        UserCondition build = UserCondition.builder().email(email).build();

        List<UserEntity> userList = userMapper.queryUser(build);
        return CollectionUtils.isEmpty(userList) ? Optional.<UserEntity>absent() : Optional.of(userList.get(0));
    }

    /**
     * 根据条件搜索用户信息
     *
     * @param searchName 查询条件
     * @param limiter    分页信息
     * @return
     */
    @Override
    public List<UserEntity> queryPage(String searchName, String userId, Limiter limiter) {

        List<UserEntity> userEntities = userMapper.queryPage(searchName, userId, limiter);

        return CollectionUtils.isEmpty(userEntities) ? Lists.<UserEntity>newArrayList() : userEntities;
    }

    @Override
    public List<UserEntity> queryByIdList(List<Integer> idList) {
        return userMapper.queryByIdList(idList);
    }

    @Override
    public List<UserEntity> queryByUserList(List<String> userIds) {
        return userMapper.queryByUserList(userIds);
    }

    /**
     * 根据条件统计用户数量
     *
     * @param searchName
     * @return
     */
    public int count(String searchName, String userId) {
        return userMapper.count(searchName, userId);
    }

    /**
     * 根据id获取用户信息
     *
     * @param id
     * @return
     */
    public Optional<UserEntity> queryUserById(int id) {
        return Optional.fromNullable(userMapper.queryById(id));
    }

}
